package org.goyo.main.domain.dao.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.exception.DBException;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.BaseDAO;
import org.goyo.main.domain.dao.system.ISysUserDao;
import org.goyo.main.domain.entity.system.SysUser;
import org.springframework.stereotype.Repository;
@Repository
public class SysUserDaoImpl extends BaseDAO implements ISysUserDao {

	@Override
	public Pager findByCond(String cond, Pager pager) {
		pager.setSortname("createDate");
		StringBuffer buf = new StringBuffer("from SysUser where 1=1 ");
		if (StringUtils.isNotBlank(cond)) {
		  buf.append(" and ");
          buf.append(cond);
		}
		    String hql = this.addSort(buf, pager);
		    String countHql = "select count(*) "+buf.toString();
		 return this.findObjectsByPager(hql,countHql, pager);
	}

	@Override
	public SysUser findByUserName(String userName) throws DBException {
		String hql = "from SysUser where userName=?";
		return this.findUnique(hql, new Object[]{userName});
	}

	@Override
	public int findByCondCount(Map<String, Object> cond) {
		StringBuffer buf = new StringBuffer("select count(*) from SysUser where 1=1 ");
		List<Object> params = getCondition(buf, cond);
		int total = this.getTotalByCond(buf.toString(), params.toArray());
		return total;
	}
	
	
	public List<Object> getCondition(StringBuffer buf,Map<String, Object> cond){
		List<Object> params = new ArrayList<Object>();
		
		if(cond.get("userName") != null && !"".equals(cond.get("userName"))){
			buf.append(" and userName=? ");
			params.add(cond.get("userName"));
		}
		if(cond.get("") != null && !"".equals(cond.get(""))){
			buf.append("");
			params.add(cond.get(""));
		}
		return params;
	}
	
}
